﻿using System;
using System.Collections.Generic;

using System.Windows.Forms;
using Fiddler;

namespace FiddlerInspectorsEx
{
    public partial class SimulateSpeedsFrm : Form
    {
        List<string> urlList = new List<string>();

        public SimulateSpeedsFrm()
        {
            InitializeComponent();
        }

        public SimulateSpeedsFrm(int sessionId, List<string> list)
        {
            InitializeComponent();
            urlList = list;
            this.Text += "- #" + sessionId + " " + list[0];
        }

        private void SimulateSpeedsFrm_Load(object sender, EventArgs e)
        {
            foreach (var url in urlList)
            {
                if (JSONView.simulateSpeedsDic.ContainsKey(url))
                {
                    Dictionary<string, string> speeds = new Dictionary<string, string>();
                    speeds = JSONView.simulateSpeedsDic[url];
                    numericUpDown1.Value = Convert.ToDecimal(speeds["up"]);
                    numericUpDown2.Value = Convert.ToDecimal(speeds["down"]);
                } 
            }
        }

        private void Btn_SaveSpeeds_Click(object sender, EventArgs e)
        {
            if (numericUpDown1.Value == 0 && numericUpDown2.Value == 0)
            {
                this.Close();
                return;
            }
                
            foreach (string item in urlList)
            {
                Dictionary<string, string> speeds = new Dictionary<string, string>();
                speeds.Add("up", numericUpDown1.Value.ToString());
                speeds.Add("down", numericUpDown2.Value.ToString());
                if (JSONView.simulateSpeedsDic.ContainsKey(item))
                {
                    JSONView.simulateSpeedsDic[item] = speeds;
                }
                else
                {
                    JSONView.simulateSpeedsDic.Add(item, speeds);
                }
                FiddlerApplication.Log.LogString(string.Format("已添加接口弱网：{0} Up：{1}ms/KB Down：{2}ms/KB", item, numericUpDown1.Value.ToString(), numericUpDown2.Value.ToString()));
            }
            this.Close();
        }
    }
}
